/*
 * Copyright 2010 Electronic Business Systems Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package gr.ebs.gssdroid.widget;

import gr.ebs.gssdroid.GssDroid;
import gr.ebs.gssdroid.R;
import gr.ebs.gssdroid.resource.BackResource;
import gr.ebs.gssdroid.resource.FileResource;
import gr.ebs.gssdroid.resource.FolderResource;
import gr.ebs.gssdroid.resource.OtherUserResource;
import gr.ebs.gssdroid.resource.RestResource;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class OthersListAdapter extends CustomListAdapter<RestResource>{
	
	public OthersListAdapter(Context context, int viewid,
			ArrayList<RestResource> listItems) {
		super(context, viewid, listItems);
		
	}
	
	@Override
	protected void bindView(final int position, View v) {
		RestResource res = getItem(position);
		ImageView imageIcon = (ImageView) v.findViewById(R.id.imageIcon);
		imageIcon.setTag("img");
		TextView textFileName = (TextView) v.findViewById(R.id.textFileName);
		textFileName.setTag("text1");
		
		TextView textFileSize = (TextView) v.findViewById(R.id.textFileSize);
		textFileSize.setTag("text2");
		ImageView operationButton =(ImageView)v.findViewById(R.id.more);
		operationButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onItemClick(position,v);
				
			}
		});
		if(res instanceof BackResource){
			imageIcon.setImageResource(R.drawable.iconup);
			textFileName.setText(((BackResource) res).getName());
			textFileSize.setText("");
			operationButton.setVisibility(View.INVISIBLE);
		}
		else if(res instanceof FolderResource){
			imageIcon.setImageResource(R.drawable.folder);
			textFileName.setText(((FolderResource) res).getName());
			if(((FolderResource) res).getModificationDate()!=null)
				textFileSize.setText("Modified : "+GssDroid.formatLocalDateTime(((FolderResource) res).getModificationDate()));
			else
				textFileSize.setText("");
			operationButton.setVisibility(View.VISIBLE);
		}
		else if(res instanceof FileResource){
			imageIcon.setImageResource(getIconForMime(((FileResource)res).getContentType()));
			textFileName.setText(((FileResource) res).getName());
			textFileSize.setText("" + ((FileResource) res).getFileSizeAsString()+" "+" Modified : "+GssDroid.formatLocalDateTime(((FileResource) res).getModificationDate()));
			operationButton.setVisibility(View.VISIBLE);
		}
		else if(res instanceof OtherUserResource){
			imageIcon.setImageResource(R.drawable.user);
			textFileName.setText(((OtherUserResource) res).getName());
			textFileSize.setText("");
			operationButton.setVisibility(View.INVISIBLE);
		}
		/*
		v.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.v("ADAPTER", "ITEM CLICK");
				onItemClick(position);
				
				
				
			}
		});*/
	}

	public void onItemClick(int position,  View v){
		
	}

}
